package com.lucio.mybatis.core.session;

import com.lucio.mybatis.core.Configuration;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.xml.sax.InputSource;

import java.io.Reader;
import java.util.List;

public class SqlSessionFactoryBuilder {

    public DefaultSqlSessionFactory build(Reader reader) {

        SAXReader saxReader = new SAXReader();
        try {
            Document document = saxReader.read(new InputSource(reader));
            Configuration configuration = parseConfig(document.getRootElement());
            return new DefaultSqlSessionFactory(configuration);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return null;
    }

    private Configuration parseConfig(Element root) {

        Configuration config = new Configuration();
        config.setDataSource(formatDataSource(root.elements("//dataSource")));

        return config;
    }

    private DataSource formatDataSource(List<Element> list) {

        DataSource dataSource = new DataSource();

        Element element = list.get(0);
        List<Node> content = element.content();
        for (Node node : content) {
        }
        return dataSource;
    }
}
